package com.hopu.phone_admin.controller;

import com.hopu.phone_admin.entity.JsonResult;
import com.hopu.phone_admin.entity.ModuleTreeNode;
import com.hopu.phone_admin.service.IModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("module")
public class ModuleController {

    @Autowired
    private IModuleService moduleService;

    @GetMapping("getModuleTreeNodes")
    public JsonResult getModuleTreeNodes(String account){
        try {
            List<ModuleTreeNode> nodes = moduleService.getModuleTreeNodeByAccount(account);
            return new JsonResult(200,"success",nodes);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(500,"failed",null);
        }
    }

    @GetMapping("getRoleModuleTreeNodes")
    public JsonResult getRoleModuleTreeNodes(String roleName){
        try {
            List<ModuleTreeNode> nodes = moduleService.getModuleTreeNodeByRoleName(roleName);
            return new JsonResult(200,"success",nodes);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(500,"failed",null);
        }
    }

    @PostMapping("grantModuleToRole")
    public JsonResult grantModuleToRole(String moduleName,String roleId){
        try {
            moduleService.grantModuleToRole(moduleName,roleId);
            return new JsonResult(200,"success",null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(500,"failed",null);
        }
    }

    @PostMapping("removeModuleFromRole")
    public JsonResult removeModuleToRole(String moduleName,String roleId){
        try {
            moduleService.removeModuleFromRole(moduleName,roleId);
            return new JsonResult(200,"success",null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(500,"failed",null);
        }
    }
}
